package com.smforj.ssm.frame.core.web.controller;

import java.util.Date;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.smforj.ssm.frame.core.bean.BaseBean;
import com.smforj.ssm.frame.core.dao.domain.Identifiable;
import com.smforj.ssm.frame.core.enums.Status;
import com.smforj.ssm.util.DateConvert;

/***
 * 公用检验方法
 * 
 * @author Haijun Gao 
 * @date 2017-5-22 下午4:11:28
 * @desp http://www.cnblogs.com/saga5998/
 * @email 573107773@qq.com
 */
public class BaseValidator implements Validator {

	@Override
	public boolean supports(Class<?> clazz) { 
		 //判断是否是要校验的类
        return BaseBean.class.equals(clazz); 
	}

	@Override
	public void validate(Object target, Errors errors) { 
		//未处理，仅仅进行了打印
		if(null != target)
		{
			BaseBean u = (BaseBean) target; 
			System.out.println(u);
		}
	}
	/****
	 * 该方法对BaseBean 进行保存判断
	 * @param bean
	 * @date 2017-5-28 下午3:50:02
	 */
	public static void vlidateForSave(Identifiable bean)
	{
		if(bean != null && bean instanceof BaseBean)
		{
			BaseBean bb = (BaseBean) bean; 
			if(bb.getCreate_date() == null)
				bb.setCreate_date(new Date());
			if(bb.getUpdate_date() == null)
				bb.setUpdate_date(DateConvert.getTimestamp());
			if(bb.getIsdeleted() == null)
				bb.setIsdeleted(false);
			if(bb.getStatus() == null)
				bb.setStatus(Status.OFF);
		} 
	}
	
	/****
	 * 该方法对BaseBean 进行更新判断
	 * @param bean
	 * @date 2017-5-28 下午3:50:02
	 */
	public static void vlidateForUpdate(Identifiable bean)
	{
		if(bean != null && bean instanceof BaseBean)
		{
			BaseBean bb = (BaseBean) bean;  
			if(bb.getUpdate_date() == null)
				bb.setUpdate_date(DateConvert.getTimestamp()); 
		} 
	}

}
